package com.pro.framework.generator.main.rename;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class RenameProperties {
    public static void main(String[] args) throws IOException {
        Path startDir = Paths.get("D:\\lzb\\demo-cloud");

        Files.walkFileTree(startDir, new SimpleFileVisitor<Path>() {
            // 在进入目录前判断是否需要跳过
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
                String pathStr = dir.toString().toLowerCase();
                if (pathStr.contains("target") || pathStr.contains("node_modules") || pathStr.contains(".git")) {
                    // 跳过该目录及其所有子目录
                    return FileVisitResult.SKIP_SUBTREE;
                }
                return FileVisitResult.CONTINUE;
            }

            // 访问文件时判断是否符合条件进行重命名
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String fileName = file.getFileName().toString();
                if (fileName.contains("message_")) {
                    Path newFile = file.resolveSibling(fileName.replace("message_", "messages_"));
                    Files.move(file, newFile);
                    System.out.println("Renamed: " + file + " -> " + newFile);
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
